<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
    String date = input.getDesc("date", "-1");
    List<Order> list = QBuilder.db("basedb").select().from("Order").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("CONCAT(FROM_UNIXTIME(registerTime/1000,'%Y-%m-%d'),'')=", date).and("state=", STATE_ADD)).order("registerTime", QBuilder.desc).query().getResultObj();
    List<Map> waits = new ArrayList<Map>();
    for (int i = 0; i < list.size(); i++) {
        Order order = list.get(i);
        Integer state = order.getState();
        Map map = new HashMap();
        map.put("patient_name", order.getUserName());
        map.put("doctor_name", order.getDoctorName());
        map.put("doctor_uid", Tools.getDesc(order.getDoctor() + "", ""));
        map.put("type", Tools.getDesc(order.getType() + "", ""));
        map.put("title", order.getTitle());
        map.put("state", state);
        map.put("register_time", DateUtil.formatDate(new Date(order.getRegisterTime())));
        map.put("exec_time", isEmpty(order.getExecTime()) ? "" : DateUtil.formatDate(new Date(order.getExecTime())));
        map.put("half", order.getHalf());
        map.put("age", order.getAge());
        map.put("phone", order.getPhone());
        map.put("start_time", order.getStartTime());
        map.put("end_time", order.getEndTime());
        map.put("order_id", order.getId());
        waits.add(map);
    }
    list = QBuilder.db("basedb").select().from("Order").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("CONCAT(FROM_UNIXTIME(execTime/1000,'%Y-%m-%d'),'')=", date).and("state in("+STATE_HANDLED+","+STATE_OVER+","+STATE_HANDLED_CANCEL+")")).order("registerTime", QBuilder.desc).query().getResultObj();
    Collections.sort(list, new Comparator<Order>() {

		@Override
		public int compare(Order o1, Order o2) {
			return Tools.getInt(o1.getState(), 0) - Tools.getInt(o2.getState(), 0);
		}
	});
    List<Map> arranges = new ArrayList<Map>();
    for (int i = 0; i < list.size(); i++) {
        Order order = list.get(i);
        Integer state = order.getState();
        Map map = new HashMap();
        map.put("patient_name", order.getUserName());
        map.put("doctor_name", order.getDoctorName());
        map.put("doctor_uid", Tools.getDesc(order.getDoctor() + "", ""));
        map.put("type", Tools.getDesc(order.getType() + "", ""));
        map.put("title", order.getTitle());
        map.put("state", state);
        map.put("register_time", DateUtil.formatDate(new Date(order.getRegisterTime())));
        map.put("exec_time", isEmpty(order.getExecTime()) ? "" : DateUtil.formatDate(new Date(order.getExecTime())));
        map.put("half", order.getHalf());
        map.put("age", order.getAge());
        map.put("phone", order.getPhone());
        map.put("start_time", order.getStartTime());
        map.put("end_time", order.getEndTime());
        map.put("order_id", order.getId());
        arranges.add(map);
    }
    Map map = new HashMap();
    map.put("waits", waits);

    map.put("arranges", arranges);
    msg.put("data", map);
    Tools.encryptPrint(out, msg);
%>
